package com.fishbone.jwt.config;

import com.fishbone.jwt.*;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.security.KeyPair;

/**
 * @ClassName FrsAutoConfiguration
 * @Description TODO
 * @Author Administrator
 * @DATE 2019/3/7 14:40
 */
@Configuration
@EnableConfigurationProperties(JwtProperties.class)
@ConditionalOnProperty(prefix = "jwt.config",name = "enabled",matchIfMissing = true)
public class JwtAutoConfiguration {

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Bean
    public JwtTokenStorage jwtTokenStorage(JwtProperties jwtProperties) {
        return new JwtTokenRedisStorage(redisTemplate,jwtProperties);
    }

    @Bean
    KeyPair keyPair(JwtProperties jwtProperties){
        KeyPairFactory keyPairFactory = new KeyPairFactory();
        return keyPairFactory.create(jwtProperties.getKeyLocation(),
                jwtProperties.getKeyAlias(),jwtProperties.getKeyPass());
    }

    @Bean
    public JwtTokenGenerator jwtTokenGenerator(JwtTokenStorage jwtTokenStorage, JwtProperties jwtProperties, KeyPair keyPair) {
        return new RSATokenGenerator(jwtTokenStorage, jwtProperties,keyPair);
    }



}
